package com.rocching.easychat.ui;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.rocching.easychat.R;
import com.rocching.easychat.adapter.FriendListAdapter;
import com.rocching.easychat.core.IApiCallback;
import com.rocching.easychat.core.ViewClickListener;
import com.rocching.easychat.model.FriendModel;
import com.rocching.easychat.model.MyFriendListModel;
import com.rocching.easychat.service.FriendService;
import com.rocching.easychat.service.GroupService;
import com.rocching.easychat.service.dto.CreateGroupInput;
import com.rocching.easychat.util.StringUtil;

import java.util.List;

public class AddGroupActivity extends AppCompatActivity {
    private final String TAG = AddGroupActivity.class.getName();
    private FriendListAdapter friendListAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_group);

        init();
    }

    private void init() {
        friendListAdapter = new FriendListAdapter(this);

        TextView selectedView = findViewById(R.id.tv_selected);
        TextView confirmView = findViewById(R.id.tv_confirm);
        ListView listView = findViewById(R.id.friend_list);

        listView.setAdapter(friendListAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                int count = friendListAdapter.selected(i, view);
                updateSelectedCount(selectedView, count);
            }
        });

        updateSelectedCount(selectedView, 0);

        initData();

        confirmView.setOnClickListener(new ViewClickListener() {
            @Override
            public void onDClick(View v) {
                handleClick();
            }
        });
    }

    private void initData() {
        FriendService.getInstance().getFriendListByUserId(new IApiCallback<MyFriendListModel>() {
            @Override
            public void onResponse(boolean succeeded, String body, MyFriendListModel data) {
                if (succeeded) {
                    updateView(data.getList());
                }
            }
        });
    }

    private void handleClick() {
        List<Long> list = null;
        if (friendListAdapter != null) {
            list = friendListAdapter.getAllSelected();
        }
        if (StringUtil.isEmpty(list)) {
            showText(getString(R.string.has_no_selected_friend));
            return;
        }

        CreateGroupInput input = new CreateGroupInput();
        input.setDescription("");
        input.setNotice("");
        input.setName(friendListAdapter.getSelectedUserName());
        input.setUserIdList(list);

        GroupService.getInstance().createGroup(input, new IApiCallback<Object>() {
            @Override
            public void onResponse(boolean succeeded, String body, Object data) {
                if (succeeded) {
                    finish();
                } else {
                    Log.e(TAG, body);
                }
            }
        });
    }

    private void updateSelectedCount(TextView view, int count) {
        if (count > 0) {
            String s = getString(R.string.has_selected_friend);
            view.setText(String.format(s, count + ""));
        } else {
            view.setText(getString(R.string.has_no_selected_friend));
        }
    }

    private void updateView(List<FriendModel> list) {
        if (StringUtil.isEmpty(list)) {
            return;
        }
        if (friendListAdapter == null) {
            return;
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                friendListAdapter.update(list);
            }
        });
    }

    private void showText(String text) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT).show();
            }
        });
    }
}